package com.longrise.co.ui.carfiles.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.carfiles.adapter.BindCarInfoAdapter;
import com.longrise.co.ui.carfiles.contract.CarFilesContract;
import com.longrise.co.ui.carfiles.model.CarFilesModel;
import com.longrise.co.ui.carfiles.presenter.CarFilesPresenter;
import com.longrise.co.ui.home.model.BindCarInfo;
import com.longrise.co.ui.login.fragment.LoginFragment;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;

import butterknife.Bind;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by luoyang on 2017-6-26.
 */

public class CarFileFragment extends BaseFragment<CarFilesPresenter, CarFilesModel> implements CarFilesContract.View<BindCarInfo>, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {


    @Bind(R.id.head_back)
    RelativeLayout headBack;
    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.search_no_data_tv)
    TextView searchNoDataTv;
    @Bind(R.id.no_data_tip)
    RelativeLayout noDataTip;
    @Bind(R.id.car_file_login_root)
    RelativeLayout carFileLogin;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private BindCarInfoAdapter mAdapter;

    private boolean isFirstLoad = true;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_main_car_file;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        headBack.setVisibility(View.GONE);
        headTitle.setText("汽车维修档案");
        rightBtn.setVisibility(View.VISIBLE);
        searchNoDataTv.setText("您暂无绑定车辆");
        noDataTip.setVisibility(View.GONE);
        initRecyclerView();
        mRxManager.on(Constants.RX_EVENT_REFRESH_CAR_FILE, new Action1<String>() {
            @Override
            public void call(String s) {
                if (checkLogin() && "loginSuccess".equals(s)) {
                    noDataTip.setVisibility(View.GONE);
                    mPresenter.getBindCarList(null, true);
                    return ;
                }
                isFirstLoad = true;
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isFirstLoad) {
            Log.i("CarFileFragment", "coming CarFileFragment....");
            isFirstLoad = false;
            if (checkLogin()) {
                noDataTip.setVisibility(View.GONE);
                mPresenter.getBindCarList(null, true);
            }
        }
    }

    private boolean checkLogin() {
        boolean isLogin = CommonPara.getInstance().isLogin();
        carFileLogin.setVisibility(isLogin ? View.GONE : View.VISIBLE);
        return isLogin;
    }

    private void initRecyclerView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Constants.RECYCLER_RGB);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new BindCarInfoAdapter(getContext(), null);

        mAdapter.openLoadMore(ApiConstants.PAGE_SIZE_10, true);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        recyclerView.setAdapter(mAdapter);
    }

    public static CarFileFragment newInstance() {
        return new CarFileFragment();
    }

    @Override
    public void requestSuccess(BindCarInfo bindCarInfo) {
        if (bindCarInfo != null && bindCarInfo.getList() != null && bindCarInfo.getList().size() > 0) {
            mAdapter.setNewData(bindCarInfo.getList());
            mAdapter.notifyDataChangedAfterLoadMore(false);
        } else {
            //没有搜索到结果
            noDataTip.setVisibility(View.VISIBLE);
            mAdapter.notifyDataChangedAfterLoadMore(false);
        }
    }

    @Override
    public void onComplete() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }


    @Override
    public void onRefresh() {
        if (checkLogin()) {
            noDataTip.setVisibility(View.GONE);
            mPresenter.getBindCarList(null, false);
        }
    }

    @Override
    public void onItemClick(View view, int i) {
        if (mAdapter.getData() != null && i < mAdapter.getData().size() && i >= 0) {
            if(mAdapter.getData().get(i).getRepairnum()>0){
            RepairRecordFragment rrf = RepairRecordFragment.newInstance(mAdapter.getData().get(i));
            mRxManager.post(Constants.RX_EVENT_NEXT_PAGE, rrf);
            }
        }
    }

    @OnClick(R.id.car_file_login_btn)
    public void onViewClicked() {
        mRxManager.post(Constants.RX_EVENT_NEXT_PAGE, LoginFragment.newInstance());
    }
}
